package pl.kosmowska.ewelina.client.app.activity;


import java.util.Date;
import java.util.List;

import pl.kosmowska.ewelina.client.ViewMessages;
import pl.kosmowska.ewelina.client.app.ClientFactory;
import pl.kosmowska.ewelina.client.app.activity.places.IncomeOutcomePlace;
import pl.kosmowska.ewelina.client.app.activity.places.SignInPlace;
import pl.kosmowska.ewelina.client.app.requestfactory.dto.OutcomeDataProxy;
import pl.kosmowska.ewelina.client.app.user.AuthEvent;
import pl.kosmowska.ewelina.client.app.user.AuthEvent.Auth;
import pl.kosmowska.ewelina.client.app.user.AuthEventHandler;
import pl.kosmowska.ewelina.client.views.IncomeOutcomeView;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class IncomeOutcomeActivity extends AbstractActivity implements IncomeOutcomeView.Presenter {

  private IncomeOutcomeView view;

  private ClientFactory clientFactory;
  

  public IncomeOutcomeActivity(IncomeOutcomePlace place, ClientFactory clientFactory) {
    this.clientFactory = clientFactory;
  }

 
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		view = clientFactory.getIncomeOutcomeView();
		view.setPresenter(this);
		initAuthorisationForView();
		startView();
		containerWidget.setWidget(view.asWidget());
	}


	private void startView() {
		if (clientFactory.getIsLoggedIn() == null) {
			// wait for login event b/c hasn't happened yet
		} else if (clientFactory.getIsLoggedIn() == true) {
			loadDataForIncomeOutcomeView();
		} else if (clientFactory.getIsLoggedIn() == false) { 
			goTo(new SignInPlace());
		}
	}

	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}


	private void initAuthorisationForView() {
		if (view.isAlreadyInit() == false) {
			clientFactory.getEventBus().addHandler(AuthEvent.TYPE, new AuthEventHandler() {
				public void onAuthEvent(AuthEvent event) {
					Auth e = event.getAuthEvent();
					if (e == Auth.LOGGEDIN) {
						loadDataForIncomeOutcomeView();
					} else if (e == Auth.LOGGEDOUT) {
						goTo(new SignInPlace());
					}
				}
			});
		}
		view.setAlreadyInit(true);
	}

  
	@Override
	public void loadDataForIncomeOutcomeView(Date startDate, Date endDate) {
		clientFactory.getAppLoadingView().startProcessing();
		Request<List<OutcomeDataProxy>> req = clientFactory.getRequestFactory().getOutcomeDataRequest().findOutcomeDataByUserBetweenDates(startDate, endDate);
		req.fire(new Receiver<List<OutcomeDataProxy>>() {
			public void onSuccess(List<OutcomeDataProxy> returnedOutcomeData) {
				processOutcomeList(returnedOutcomeData);
				clientFactory.getAppLoadingView().stopProcessing();
			}
			public void onFailure(ServerFailure error) {
				super.onFailure(error);
				clientFactory.getAppLoadingView().stopProcessing();
			}
		});

	}
	
	@Override
	public void loadDataForIncomeOutcomeView() {
		clientFactory.getAppLoadingView().startProcessing();
		Receiver<List<OutcomeDataProxy>> receiver= new Receiver<List<OutcomeDataProxy>>() {
			public void onSuccess(List<OutcomeDataProxy> returnedOutcomeData) {
				processOutcomeList(returnedOutcomeData);
				clientFactory.getAppLoadingView().stopProcessing();
			}
			public void onFailure(ServerFailure error) {
				super.onFailure(error);
				clientFactory.getAppLoadingView().stopProcessing();
			}
		};
		Request<List<OutcomeDataProxy>> req = clientFactory.getRequestFactory().getOutcomeDataRequest().findOutcomeDataByUser(new Date());
		req.fire(receiver);

	}
	
	protected void processOutcomeList(List<OutcomeDataProxy> returnedOutcomeData) {
		if (returnedOutcomeData == null || returnedOutcomeData.size() == 0) {
			view.showMessage(ViewMessages.NO_DATA_TO_SHOW);
			view.clearView();
		}else{
			float incomeSum = 0;
			float outcomeSum = 0;
			float balanceSum = 0;
			for(OutcomeDataProxy outcomeDataProxy: returnedOutcomeData ){
				if(outcomeDataProxy.getValue()>0){
					incomeSum+=outcomeDataProxy.getValue();
				}else{
					outcomeSum+=outcomeDataProxy.getValue();
				}
				balanceSum+=outcomeDataProxy.getValue();
			}
			incomeSum = round(incomeSum);
			outcomeSum = round(outcomeSum);
			balanceSum = round(balanceSum);

			view.reInitView(balanceSum, incomeSum, outcomeSum , returnedOutcomeData);
		}

	}
	
	private float round (double val)
	{
		double exp = Math.pow(10, 2);
		double rounded = val * exp;
		rounded = Math.round(rounded);
		return (float) (rounded / exp);
	}
}


